package springboot.Controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 文件上传
 */
@RestController
public class FileController {
    @PostMapping(value = "/upload")
    public String upload(@RequestParam(value = "file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String suffName = fileName.substring(fileName.lastIndexOf("."));
        String filePath="C:/image/";
        fileName=UUID.randomUUID().toString().replaceAll("-","")+suffName;
        File file1 = new File(filePath + fileName);
        if (!file1.getParentFile().exists()){
            file1.getParentFile().mkdirs();
        }
        try {
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;

    }
}
